<template>
  <div>
    <h1>系统日志</h1>
    <div>
      <button @click="fetchLogs('info')">Info Logs</button>
      <button @click="fetchLogs('debug')">Debug Logs</button>
      <button @click="fetchLogs('error')">Error Logs</button>
    </div>
    <div v-if="logs.length > 0">
      <h2>{{ currentLogType }} Logs</h2>
      <pre>{{ logs }}</pre>
    </div>
    <div v-else>
      <p>No logs to display.</p>
    </div>
  </div>
</template>

<script>
import Instance from '@/services/instance';
import {VIEW_GROUP} from '../../index';

export default {
  props: {
    instance: {
      type: Instance,
      required: true
    }
  },
  data() {
    return {
      logs: [],
      currentLogType: ''
    };
  },
  methods: {
    async fetchLogs(logType) {
      let serviceUrl = this.instance.registration.managementUrl
      try {
        let res = await this.instance.fetchLogFile(serviceUrl, logType);
        if (res.status === 200) {
          this.logs = res.data.join('\n');
          this.currentLogType = logType;
        } else {
          this.logs = [];
          this.currentLogType = '';
        }
      } catch (error) {
        this.logs = [];
        this.currentLogType = '';
      }
    }
  },
  // install({viewRegistry}) {
  //   viewRegistry.addView({
  //     name: 'instances/logs',
  //     parent: 'instances',
  //     path: 'logs',
  //     component: this,
  //     label: '系统日志',
  //     group: VIEW_GROUP.LOGGING,
  //     order: 190,
  //     isEnabled: true
  //   });
  install({viewRegistry}) {
    viewRegistry.addView({
      name: 'instances/logs',
      parent: 'instances',
      path: 'logs',
      label: '系统日志',
      group: VIEW_GROUP.INSIGHTS,
      component: this,
      order: 110,
      isEnabled: ({instance}) => instance.hasEndpoint('beans')
    });
  }
}
</script>

<style lang="scss">
@import "~@/assets/css/utilities";

.pre {
  background-color: #f4f4f4;
  padding: 10px;
  border-radius: 5px;
  overflow-x: auto;
}
</style>
